﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Code.Models;
using Code.Commen;
using System.Security.Cryptography;

namespace Code.BLL
{
    /// <summary>
    /// 缓存
    /// </summary>
    public class CacheManage
    {  
        #region 有关域名注册
        /// <summary>
        /// 是否重置域名注册的缓存
        /// </summary>
        public static bool IsLoadRegest { get; set; }
        private static DateTime _RegestTime;
        private static string _RegestStr;
        public static string RegestStr
        {
            get 
            {
                if (IsLoadRegest || string.IsNullOrEmpty(_RegestStr) || _RegestTime.AddMinutes(OptionSite[Option_Site_Key.Site_Cache_OptionsTime].ToDouble(1)) <= DateTime.Now)
                {
                    IsLoadRegest = false;
                    _RegestTime = DateTime.Now;
                    _RegestStr = new SiteManage().Select(s => s.Where(p => p.Key == "Site_RegestCode").Select(p => p.Value).FirstOrDefault()) ?? "";
                }
                return _RegestStr;
            }
        }
        /// <summary>
        /// 判断当前域名是否有注册
        /// </summary>
        /// <param name="name">要判断的域名</param>
        /// <returns></returns>
        public static bool IsRegest(string name)
        {
            var str = MD5.Create().ComputeHash(name, Salt.Keys.GetMd5Salt());
            return RegestStr.Contains(str);
        }
        #endregion

        #region 有关提款
        /// <summary>
        /// 是否立即刷新提款申请数量
        /// </summary>
        public static bool IsLoadTakeMoneyCount { get; set; }
        private static DateTime _loadTakeMoneyCount_Time;
        private static int _TakeMoneyUnDisposeCount;
        /// <summary>
        /// 未处理提款申请条数
        /// </summary>
        public static int TakeMoneyUnDisposeCount 
        {
            get 
            {
                if (IsLoadTakeMoneyCount || _loadTakeMoneyCount_Time.AddMinutes(OptionSite[Option_Site_Key.Site_Cache_TakeMoneyTime].ToDouble(1)) < DateTime.Now)
                {
                    IsLoadTakeMoneyCount = false;
                    _loadTakeMoneyCount_Time = DateTime.Now;
                    var count = new TakeMoneyManage().GetTakeMoneyUnDisposeCount(null);
                    if (count > _TakeMoneyUnDisposeCount)
                        NewTakeMoneyUnDisposeCount = count - _TakeMoneyUnDisposeCount;
                    _TakeMoneyUnDisposeCount = count;
                }
                return _TakeMoneyUnDisposeCount;
            }
            set 
            {
                _TakeMoneyUnDisposeCount = value;
            }
        }
        /// <summary>
        /// 新增加的提款申请条数
        /// </summary>
        public static int NewTakeMoneyUnDisposeCount { get; set; }
        #endregion

        #region 其它
        /// <summary>
        /// 是否重置站点设置的缓存
        /// </summary>
        public static bool IsLoadOptionSite { get; set; }

        public static IEnumerable<Option_Site_Key> DBKeys { get { return _keys; } }
        /// <summary>
        /// 系统设置
        /// </summary>
        public static HashTable<Option_Site_Key, Option_Site> OptionSite
        {
            get
            {
                if (IsLoadOptionSite || _optionsite == null || _optionsite_time.AddMinutes(_optionsite[Option_Site_Key.Site_Cache_OptionsTime].ToDouble(1)) <= DateTime.Now)
                {
                    IsLoadOptionSite = false;
                    _optionsite_time = DateTime.Now;
                    var tmp = (Enum.GetValues(typeof(Option_Site_Key)) as Option_Site_Key[]).ToDictionary(p => p, p => p.ToString());
                    var ops = new SiteManage().Select(s => s.Where(p => tmp.Values.Contains(p.Key)).ToArray());
                    _keys = ops.Select(p => p.EnumName);

                    _optionsite = ops.Where(p => tmp.Keys.Select(pp => pp.ToString()).Contains(p.Key)).ToHashTable(p => p.EnumName, p => p);
                    tmp.Where(p => !_optionsite.Keys.Contains(p.Key)).Each(p => _optionsite.Add(p.Key, new Option_Site { Key = p.Key.ToString() }));
                }
                return CacheManage._optionsite;
            }
        }
        private static DateTime _optionsite_time;
        private static HashTable<Option_Site_Key, Option_Site> _optionsite;
        private static IEnumerable<Option_Site_Key> _keys;

        /// <summary>
        /// 立即刷新彩种信息
        /// </summary>
        public static bool IsLoadLottery { get; set; }
        /// <summary>
        /// 彩种
        /// </summary>
        public static HashTable<LotteryType, Lottery> Lottery
        {
            get
            {
                if (IsLoadLottery || _lottery == null || _lottery_time.AddMinutes(OptionSite[Option_Site_Key.Site_Cache_LotteryTime].ToDouble(1)) <= DateTime.Now)
                {
                    IsLoadLottery = false;
                    _lottery_time = DateTime.Now;
                    _lottery = new LotteryManage().Select(s => s.OrderBy(p => p.ID).ToHashTable(p => (LotteryType)p.ID, p => p));
                    var state = System.Configuration.ConfigurationManager.AppSettings["LuckyTurnable"];
                    if (!"Enabled".Equals(state))
                        _lottery.Remove(LotteryType.LuckyTT);
                }
                return CacheManage._lottery;
            }
        }
        private static DateTime _lottery_time;
        private static HashTable<LotteryType, Lottery> _lottery;

        /// <summary>
        /// 立即刷新彩种信息
        /// </summary>
        public static bool IsLoadLotteryTypes { get; set; }
        /// <summary>
        /// 彩种
        /// </summary>
        public static HashTable<LotteryTypeType, LotteryTypes> LotteryTypes
        {
            get
            {
                if (IsLoadLotteryTypes || _lotterytypes == null || _lotterytypes_time.AddMinutes(OptionSite[Option_Site_Key.Site_Cache_LotteryTime].ToDouble(1)) <= DateTime.Now)
                {
                    IsLoadLotteryTypes = false;
                    _lotterytypes_time = DateTime.Now;
                    _lotterytypes = new LotteryManage().Select<LotteryTypes, IEnumerable<LotteryTypes>>(s => s).ToHashTable(p => (LotteryTypeType)p.ID, p => p);
                    var state = System.Configuration.ConfigurationManager.AppSettings["LuckyTurnable"];
                    if (!"Enabled".Equals(state))
                        _lotterytypes.Remove(LotteryTypeType.LuckyTT);
                }
                return CacheManage._lotterytypes;
            }
        }
        private static DateTime _lotterytypes_time;
        private static HashTable<LotteryTypeType, LotteryTypes> _lotterytypes;

        /// <summary>
        /// 立即刷新玩法信息
        /// </summary>
        public static bool IsLoadPlayList { get; set; }
        /// <summary>
        /// 玩法
        /// </summary>
        public static HashTable<PlayType_E, PlayTypeList> Playlist
        {
            get
            {
                if (IsLoadPlayList || _playlist == null || _playlist_time.AddMinutes(OptionSite[Option_Site_Key.Site_Cache_PlayListTime].ToDouble(1)) <= DateTime.Now)
                {
                    IsLoadPlayList = false;
                    _playlist_time = DateTime.Now;
                    _playlist = new PlayTypeManage().Select<PlayTypeList, HashTable<PlayType_E, PlayTypeList>>(s => s.ToHashTable(p => (PlayType_E)p.ID, p => p));
                }
                return CacheManage._playlist;
            }
        }
        private static DateTime _playlist_time;
        private static HashTable<PlayType_E, PlayTypeList> _playlist;

        /// <summary>
        /// 立即刷新玩法信息
        /// </summary>
        public static bool IsLoadPlayType { get; set; }
        /// <summary>
        /// 彩种玩法
        /// </summary>
        public static HashTable<int, PlayType> PlayType
        {
            get
            {
                if (IsLoadPlayType || _playtype == null || _playtype_time.AddMinutes(OptionSite[Option_Site_Key.Site_Cache_PlayTypeTime].ToDouble(1)) <= DateTime.Now)
                {
                    IsLoadPlayType = false;
                    _playtype_time = DateTime.Now;
                    _playtype = new PlayTypeManage().Select(s => s).ToHashTable(p => p.ID, p => p);
                }
                return CacheManage._playtype;
            }
        }
        private static DateTime _playtype_time;
        private static HashTable<int, PlayType> _playtype;
        
        /// <summary>
        /// 是否立即刷新返点类型数据缓存
        /// </summary>
        public static bool IsLoadBackMoneyType { get; set; }
        /// <summary>
        /// 返点类型
        /// </summary>
        public static HashTable<int, BackMoneyType> BackMoneyType
        {
            get
            {
                if (IsLoadBackMoneyType || _backMoneyType == null || _backMoneyType_Time.AddMinutes(OptionSite[Option_Site_Key.Site_Cache_PlayTypeTime].ToDouble(1)) <= DateTime.Now)
                {
                    IsLoadBackMoneyType = false;
                    _backMoneyType_Time = DateTime.Now;
                    _backMoneyType = new CommenManage<BackMoneyType>().Select(s => s).ToHashTable(p => p.ID, p => p);
                    
                }
                return _backMoneyType;
            }
        }
        private static DateTime _backMoneyType_Time;
        private static HashTable<int, BackMoneyType> _backMoneyType;
       
        /// <summary>
        /// 是否重置消费比列的缓存
        /// </summary>
        public static bool IsLoadRatio { get; set; }
        /// <summary>
        /// 消费比列
        /// </summary>
        public static HashTable<decimal, ConsumptionRatio> Ratio
        {
            get
            {
                if (IsLoadRatio || _ratio == null || _ratio_time.AddMinutes(OptionSite[Option_Site_Key.Site_Cache_RatioTime].ToDouble(1)) <= DateTime.Now)
                {
                    IsLoadRatio = false;
                    _ratio_time = DateTime.Now;
                    _ratio = new ConsumeRatioManage().Select(s => s.ToHashTable(p => p.Ratio, p => p));
                }
                return CacheManage._ratio;
            }
        }
       
        private static DateTime _ratio_time;
        private static HashTable<decimal, ConsumptionRatio> _ratio;
        public static HashTable<long, PlayTypeOption_User> PlayTypeOption
        {
            get
            {
                if (IsLoadBackMoneyType || _playTypeOption == null || _playTypeOption_Time.AddMinutes(OptionSite[Option_Site_Key.Site_Cache_PlayTypeTime].ToDouble(1)) <= DateTime.Now)
                {
                    IsLoadBackMoneyType = false;
                    _playTypeOption_Time = DateTime.Now;
                    _playTypeOption = new CommenManage<PlayTypeOption_User>().Select(s => s).ToHashTable(p => p.ID, p => p);

                }
                return _playTypeOption;
            }
        }
        private static DateTime _playTypeOption_Time;
        private static HashTable<long, PlayTypeOption_User> _playTypeOption;
        /// <summary>
        /// 是否重置奖金模式的缓存
        /// </summary>
        public static bool IsLoadWinModel { get; set; }
        /// <summary>
        /// 奖金模式
        /// </summary>
        public static HashTable<int, WinMoneyModel> WinModel
        {
            get
            {
                if (IsLoadWinModel || _winmodel == null || _winmodel_time.AddMinutes(OptionSite[Option_Site_Key.Site_Cache_WinModelTime].ToDouble(1)) <= DateTime.Now)
                {
                    IsLoadWinModel = false;
                    _winmodel_time = DateTime.Now;
                    _winmodel = new WinModelManage().Select(s => s.ToHashTable(p => p.ID, p => p));
                    _winModel_name = new WinModelManage().Select(s => s.ToHashTable(p => p.Name, p => p));
                }
                return CacheManage._winmodel;
            }
        }
        private static DateTime _winmodel_time;
        private static HashTable<int, WinMoneyModel> _winmodel;
        private static HashTable<string, WinMoneyModel> _winModel_name;

        /// <summary>
        /// 是否刷新权限设置
        /// </summary>
        public static bool IsLoadAuthority { get; set; }
        /// <summary>
        /// 权限
        /// </summary>
        public static HashTable<int, Authority> Authority
        {
            get
            {
                if (IsLoadAuthority || _authority == null || _authority_time.AddMinutes(OptionSite[Option_Site_Key.Site_Cache_AuthenticationTime].ToDouble(1)) <= DateTime.Now)
                {
                    IsLoadAuthority = false;
                    _authority_time = DateTime.Now;
                    _authority = new CommenManage<Authority>().Select(s => s.ToHashTable(p => p.ID, p => p));
                }
                return CacheManage._authority;
            }
        }
        private static DateTime _authority_time;
        private static HashTable<int, Authority> _authority;

        /// <summary>
        /// 是否立即刷新角色权限设置
        /// </summary>
        public static bool IsLoadAuthentication { get; set; }
        /// <summary>
        /// 授权
        /// </summary>
        public static HashTable<int, Authentication> Authentication
        {
            get
            {
                if (IsLoadAuthentication || _authentication == null || _authentication_time.AddMinutes(OptionSite[Option_Site_Key.Site_Cache_AuthenticationTime].ToDouble(1)) <= DateTime.Now)
                {
                    IsLoadAuthentication = false;
                    _authentication_time = DateTime.Now;
                    var tmp = new CommenManage<Authentication>().Select(s => s.Select(p => new { p.ID, p.AuthorityID, p.RolesID, p.State }).ToArray());
                    _authentication = tmp.Select(p => new Authentication { ID = p.ID, AuthorityID = p.AuthorityID, RolesID = p.RolesID, State = p.State })
                        .Join(Authority, p => p.AuthorityID, p => p.Key, (p1, p2) => { p1.Authority = p2.Value; return p1; })
                        .ToHashTable(p => p.ID, p => p);
                }
                return CacheManage._authentication;
            }
        }
        private static DateTime _authentication_time;
        private static HashTable<int, Authentication> _authentication;

        /// <summary>
        /// 是否立即刷新角色设置
        /// </summary>
        public static bool IsLoadRoles { get; set; }
        /// <summary>
        /// 角色
        /// </summary>
        public static HashTable<int, Roles> Roles
        {
            get
            {
                if (IsLoadRoles || _roles == null || _roles_time.AddMinutes(OptionSite[Option_Site_Key.Site_Cache_AuthenticationTime].ToDouble(1)) <= DateTime.Now)
                {
                    IsLoadRoles = false;
                    _roles_time = DateTime.Now;
                    _roles = new CommenManage<Roles>().Select(s => s.ToHashTable(p => p.ID, p => p));
                }
                return CacheManage._roles;
            }
        }
        private static DateTime _roles_time;
        private static HashTable<int, Roles> _roles;

        /// <summary>
        /// 是否立即刷新系统公告
        /// </summary>
        public static bool IsLoadNotices { get; set; }

        /// <summary>
        /// 公告 最新的10条公告
        /// </summary>
        public static HashTable<long, Notice> TopNotices
        {
            get
            {
                RefishNotice();
                return CacheManage._notices;
            }
        }
        /// <summary>
        /// 公告条数
        /// </summary>
        public static int NoticeCount
        {
            get
            {
                RefishNotice();
                return CacheManage._noticecount.Value;
            }
        }
        private static void RefishNotice()
        {
            if (IsLoadNotices || _noticecount == null || _notices == null || _notice_time.AddMinutes(OptionSite[Option_Site_Key.Site_Cache_NoticeTime].ToDouble(1)) <= DateTime.Now)
            {
                IsLoadNotices = false;
                _notice_time = DateTime.Now;
                var ns = new CommenManage<Notice>().Select(s => s.Where(p => p.State == (short)NoticeState.Show));
                _notices = ns.OrderByDescending(p => p.AddTime).Take(10).ToHashTable(p => p.ID, p => p);
                _noticecount = ns.Count();
            }
        }
       


        private static DateTime _notice_time;
        private static int? _noticecount;
        private static HashTable<long, Notice> _notices;

        #endregion
    }
}
